package cn.itsource.system.controller;

import cn.itsource.system.domain.Employee;
import cn.itsource.system.query.EmployeeQuery;
import cn.itsource.system.service.IEmployeeService;
import cn.itsource.until.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 * */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有
     * */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> getAll(){
        return employeeService.getAll();
    }

    /**
     * 查询单个
     * */
    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Employee getOne(@PathVariable("id")Long id){
        return employeeService.getOne(id);
    }

    /**
     * 删除单个
     * */
    @ApiOperation(value = "删除单个")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            }else employeeService.update(employee);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
            employeeService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * */
    @ApiOperation(value = "分页查询+高级查询")
    @PostMapping
    public PageInfo<Employee> pageQuery(@RequestBody EmployeeQuery query){
        return employeeService.pageQuery(query);
    }


}
